package cn.iocoder.yudao.module.dbit.controller.admin.noticeinfo;

import cn.iocoder.yudao.module.dbit.controller.admin.policynew.vo.StudentPolicyNewReqVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;


import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.dbit.controller.admin.noticeinfo.vo.*;
import cn.iocoder.yudao.module.dbit.dal.dataobject.noticeinfo.NoticeInfoDO;
import cn.iocoder.yudao.module.dbit.service.noticeinfo.NoticeInfoService;

@Tag(name = "管理后台 - 机构公告")
@RestController
@RequestMapping("/dbit/notice-info")
@Validated
public class NoticeInfoController {

    @Resource
    private NoticeInfoService noticeInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建机构公告")
    @PreAuthorize("@ss.hasPermission('dbit:notice-info:create')")
    public CommonResult<Integer> createNoticeInfo(@Valid @RequestBody NoticeInfoSaveReqVO createReqVO) {
        return success(noticeInfoService.createNoticeInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新机构公告")
    @PreAuthorize("@ss.hasPermission('dbit:notice-info:update')")
    public CommonResult<Boolean> updateNoticeInfo(@Valid @RequestBody NoticeInfoSaveReqVO updateReqVO) {
        noticeInfoService.updateNoticeInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除机构公告")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dbit:notice-info:delete')")
    public CommonResult<Boolean> deleteNoticeInfo(@RequestParam("id") Integer id) {
        noticeInfoService.deleteNoticeInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得机构公告")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('dbit:notice-info:query')")
    public CommonResult<NoticeInfoRespVO> getNoticeInfo(@RequestParam("id") Integer id) {
        NoticeInfoDO noticeInfo = noticeInfoService.getNoticeInfo(id);
        return success(BeanUtils.toBean(noticeInfo, NoticeInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得机构公告分页")
//    @PreAuthorize("@ss.hasPermission('dbit:notice-info:query')")
    public CommonResult<PageResult<NoticeInfoRespVO>> getNoticeInfoPage(@Valid NoticeInfoPageReqVO pageReqVO) {
        PageResult<NoticeInfoDO> pageResult = noticeInfoService.getNoticeInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, NoticeInfoRespVO.class));
    }
    @GetMapping("/latest")
    @Operation(summary = "学生端获取n条最新的机构公告")
    public CommonResult<List<StudentNoticeReqVO>> getLatestNoticeInfo(@RequestParam("number") Integer number) {
        List<StudentNoticeReqVO> result = noticeInfoService.getLatestNoticeInfo(number);
        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出机构公告 Excel")
    @PreAuthorize("@ss.hasPermission('dbit:notice-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportNoticeInfoExcel(@Valid NoticeInfoPageReqVO pageReqVO,
                                      HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<NoticeInfoDO> list = noticeInfoService.getNoticeInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "机构公告.xls", "数据", NoticeInfoRespVO.class,
                BeanUtils.toBean(list, NoticeInfoRespVO.class));
    }

}